﻿using System;
using System.Collections.Generic;
using System.Linq;
using IGM.DataProvider;
using IGM.Framework.Constants;

namespace IGM.Business
{
    public partial class Business
    {
        #region -- Get User --

        public List<User> GetListUser()
        {
            return DataContex.Users.ToList();
        }

        public List<User> GetListUserByKeyword(string key)
        {
            return string.IsNullOrEmpty(key)
                       ? DataContex.Users.ToList()
                       : DataContex.Users.Where(s => s.Name.ToLower().Contains(key.ToLower())).ToList();
        }
        #endregion

        #region Common

        private static bool UserCommonValidator(User user, out string message)
        {
            if (string.IsNullOrWhiteSpace(user.Name))
            {
                message = string.Format(ConstantMessages.RequireFieldIsBlank, "tên Đăng nhập");
                return false;
            }

            if (string.IsNullOrWhiteSpace(user.Password))
            {
                message = string.Format(ConstantMessages.RequireFieldIsBlank, "mật khẩu");
                return false;
            }

            if (user.Name.Length >= 50)
            {
                message = string.Format(ConstantMessages.FieldDataOverMaxLength, "tên Đăng nhập", 50);
                return false;
            }

            if (user.Password.Length > 8)
            {
                message = string.Format(ConstantMessages.FieldDataOverMaxLength, "mật khẩu", 80);
                return false;
            }

            if (user.Permission < 0 || user.Permission > 3)
            {
                message = string.Format(ConstantMessages.DataNotValid, "quyền hạn");
                return false;
            }

            message = string.Empty;
            return true;
        }

        public int UserValidator(User user)
        {
            if (DataContex.Users.Any(u => u.Name == user.Name && u.Password == user.Password))
                return DataContex.Users.Where(u => u.Name == user.Name && u.Password == user.Password).SingleOrDefault().Permission;
            return -1;
        }

        #endregion

        #region -- Add User --

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddUserValidator(User user, out string message)
        {
            if (!UserCommonValidator(user, out message)) return false;

            if (DataContex.Users.Any(f => f.Name.ToLower() == user.Name.ToLower()))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, user.Name);
                return false;
            }

            return true;
        }

        /// <summary>
        /// This function add a new facutly into list.
        /// </summary>
        public void AddUser(User user)
        {
            DataContex.Users.AddObject(user);
            DataContex.SaveChanges();
        }

        #endregion

        #region -- Edit User --


        public bool EditUserValidator(User user, out string message)
        {
            var id = user.Id;

            if (!UserCommonValidator(user, out message)) return false;

            if (DataContex.Users.Any(m => m.Name.ToLower() == user.Name.ToLower() && m.Id != id))
            {
                message = string.Format(ConstantMessages.ItemIsExisted, user.Name);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool EditUser(User user)
        {
            var id = user.Id;
            var updateUser = DataContex.Users.SingleOrDefault(f => f.Id == id);

            if (updateUser == null)
                return false;

            updateUser.Name = user.Name;
            updateUser.Permission = user.Permission;
            updateUser.Password = user.Password;

            DataContex.SaveChanges();
            return true;
        }

        #endregion

        #region -- Delete User --

        public bool DeleteUsersValidator(IEnumerable<User> users, out string message)
        {
            const string intialize = "Người dùng: ";
            var itemsCannotIdentify = (from user in users
                                       let id = user.Id
                                       where !DataContex.Users.Any(m => m.Id == id)
                                       select user).Aggregate(intialize, (current, user) => current + (user.Name + ", "));

            if (itemsCannotIdentify != intialize)
            {
                message = string.Format(ConstantMessages.SomeItemsCanNotIdentify, itemsCannotIdentify.TrimEnd(','));
                return false;
            }

            message = string.Empty;
            return true;
        }

        /// <summary>
        /// Delete object
        /// </summary>
        public void DeleteUsers(List<User> users)
        {
            for (var i = users.Count - 1; i >= 0; i--)
            {
                var id = users[i].Id;
                var deleteUser = DataContex.Users.Single(m => m.Id == id);
                DataContex.Users.DeleteObject(deleteUser);
            }
            DataContex.SaveChanges();
        }

        #endregion

        public int GetUserPermission(User user)
        {
            //Neu password chua ma hoa
            return DataContex.Users.First(u => u.Name == user.Name && u.Password == user.Password).Permission;
        }
    }
}